<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="报警类别" prop="carnumber">
        <el-select v-model="queryParams.where.category" placeholder="请选择">
          <el-option
            v-for="(item,index) in ['安防报警','消防报警']"
            :key="index"
            :label="item"
            :value="index"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报警类别" prop="carnumber">
        <el-radio-group v-model="queryParams.where.status">
          <el-radio :label="0">未处理</el-radio>
          <el-radio :label="1">已处理</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="margin_bottom_8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:edit']"
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:remove']"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:post:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col> -->
    </el-row>
    <!-- row=>parseTime[row.alarmtime] -->
    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报警类别" align="center" prop="type" :formatter="row=>typeOptions[row.type]" />
      <el-table-column label="告警时间" :show-overflow-tooltip="true" align="center" prop="alarmtime" :formatter="row=>parseTime(row.alarmtime, '{y}-{m}-{d} {h}:{m}')" />
      <el-table-column label="告警信息" align="center" prop="context" />
      <el-table-column label="处理状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status" type="success">已处理</el-tag>
          <el-tag v-else type="danger">未处理</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="处理人" align="center" prop="dealUID" />
      <el-table-column label="处理时间" :show-overflow-tooltip="true" align="center" prop="dealtime" />
      <el-table-column label="处理结果" align="center" prop="dealresult" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="!scope.row.dealresult"
            v-hasPermi="['system:post:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >处理</el-button>
          <el-button
            v-else
            v-hasPermi="['system:post:edit']"
            size="mini"
            type="text"
            disabled
          ><span class="text_999">已处理</span> </el-button>
          <el-button
            v-hasPermi="['system:post:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form v-if="!form.id" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="报警类别" prop="type" required>
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="(item,index) in typeOptions"
              :key="index"
              :label="item"
              :value="index"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="告警时间" prop="alarmtime" required>
          <!-- <el-input v-model="form.alarmtime" placeholder="告警时间" /> -->
          <el-date-picker
            v-model="form.alarmtime"
            type="date"
            placeholder="告警时间"
          />
        </el-form-item>
        <el-form-item label="告警信息" prop="context" required>
          <el-input v-model="form.context" placeholder="告警信息" />
        </el-form-item>
        <!-- <el-form-item label="处理状态" prop="status" required>
          <el-radio-group v-model="form.status">
            <el-radio :label="0">未处理</el-radio>
            <el-radio :label="1">已处理</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处理人" prop="dealUID" required>
          <el-input-number v-model="form.dealUID" placeholder="处理人" />
        </el-form-item>
        <el-form-item label="处理时间" prop="dealtime" required>
          <el-date-picker
            v-model="form.dealtime"
            type="date"
            placeholder="处理时间"
          />
        </el-form-item>
        <el-form-item label="处理结果" prop="dealresult" required>
          <el-input v-model="form.dealresult" placeholder="几何" />
        </el-form-item> -->
      </el-form>
      <el-form v-else ref="form" :model="form" :rules="rules" label-width="80px">
        <el-input
          v-model="form.dealresult"
          type="textarea"
          :rows="5"
          placeholder="请输入内容"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addUpdateItem, queryList, deleteById, getById } from '@/api/setting/alarm'

export default {
  name: 'Post',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        where: {
          category: undefined,
          status: undefined
        }
      },
      // 表单参数
      form: { },
      // 表单校验
      rules: {
      },
      dealUIDOptions: [],
      typeOptions: ['违规停车', '异常入侵', '消防告警', '设备告警']
    }
  },
  // watch: {
  //   '$route.query': {
  //     handler(newValue) {
  //       console.log(newValue)
  //     },
  //     immediate: true
  //   }
  // },
  created() {
    this.getList()
    Object.assign(this.queryParams.where, this.$route.query)
    const { category, status } = this.$route.query
    this.queryParams.where.category = Number.isNaN(parseInt(category)) ? undefined : parseInt(category)
    this.queryParams.where.status = Number.isNaN(parseInt(status)) ? undefined : parseInt(status)
    // this.getDicts('sys_normal_disable').then(response => {
    //   this.statusOptions = response.data
    // })
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      queryList(this.queryParams).then(response => {
        this.postList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    // 岗位状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        type: 1
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.where = {}
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加岗位'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getById(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '处理告警信息'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            addUpdateItem(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            addUpdateItem({ ...this.form, status: 0 }).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除岗位编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return deleteById(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {})
    }
    /** 导出按钮操作 */
    // handleExport() {
    //   // const queryParams = this.queryParams
    //   this.$confirm('是否确认导出所有岗位数据项?', '警告', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(function() {
    //     // return exportPost(queryParams)
    //   }).then(response => {
    //     this.download(response.msg)
    //   }).catch(function() {})
    // }
  }
}
</script>
